﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.ViewModel;
using OFB.Domain;
using OFB.BLL.Context;
using System.Data;

namespace OFB.BLL.Repository
{
    public class CourseSubjectBLL: ICourseSubjectBLL
    {
        public CourseSubjectContext context;

        public CourseSubjectBLL()
        {
            context = new CourseSubjectContext();
        }

        public ICollection<CourseSubjectViewModel> FindAll() { return null; }

        public CourseSubjectsViewModel Retrieve(int courseId, string subjectCode,
                                                                string subjectName, string groupName, int Offset, int MaxItem)
        {
            CourseSubjectsViewModel CourseSubjects = new CourseSubjectsViewModel();

            IDataParameter CourseSubjectCountParam = context.CourseSubject.BuildParameter("CourseSubjectCount", DbType.Int32, ParameterDirection.Output);

            CourseSubjects.CourseSubjects = context.CourseSubject.Init("CourseSubject_Select",
                                                                        context.CourseSubject.BuildParameter("CourseId", DbType.Int32, courseId),
                                                                        context.CourseSubject.BuildParameter("SubjectCode", DbType.String, subjectCode),
                                                                        context.CourseSubject.BuildParameter("SubjectName", DbType.String, subjectName),
                                                                        context.CourseSubject.BuildParameter("GroupName", DbType.String, groupName),
                                                                        context.CourseSubject.BuildParameter("Offset", DbType.Int32, Offset),
                                                                        context.CourseSubject.BuildParameter("MaxItem", DbType.Int32, MaxItem),
                                                                        CourseSubjectCountParam)
                        .MapCollection<CourseSubjectViewModel>()
                        .GetCollection();

            CourseSubjects.CourseSubjectCount = (int)CourseSubjectCountParam.Value;

            return CourseSubjects;
        }

        public CourseSubjectsViewModel RetrieveAll(int courseId, string subjectCode,
                                                                string subjectName, string groupName, int Offset, int MaxItem)
        {
            CourseSubjectsViewModel CourseSubjects = new CourseSubjectsViewModel();

            IDataParameter CourseSubjectCountParam = context.CourseSubject.BuildParameter("CourseSubjectCount", DbType.Int32, ParameterDirection.Output);

            CourseSubjects.CourseSubjects = context.CourseSubject.Init("CourseSubject_SelectAll",
                                                                        context.CourseSubject.BuildParameter("CourseId", DbType.Int32, courseId),
                                                                        context.CourseSubject.BuildParameter("SubjectCode", DbType.String, subjectCode),
                                                                        context.CourseSubject.BuildParameter("SubjectName", DbType.String, subjectName),
                                                                        context.CourseSubject.BuildParameter("GroupName", DbType.String, groupName),
                                                                        context.CourseSubject.BuildParameter("Offset", DbType.Int32, Offset),
                                                                        context.CourseSubject.BuildParameter("MaxItem", DbType.Int32, MaxItem),
                                                                        CourseSubjectCountParam)
                        .MapCollection<CourseSubjectViewModel>()
                        .GetCollection();

            CourseSubjects.CourseSubjectCount = (int)CourseSubjectCountParam.Value;

            return CourseSubjects;
        }

        public int Delete(int CourseSubjectId)
        {
            IDataParameter rowEffectedParam = context.CourseSubject.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);

            context.CourseSubject.Execute("CourseSubject_Delete", context.CourseSubject.BuildParameter("CourseSubjectId", DbType.Int32, CourseSubjectId),
                                                                  rowEffectedParam);

            return (int)rowEffectedParam.Value;
        }

        public int Add(int courseId, int subjectId, int groupId)
        {
            IDataParameter courseSubjectIdParam = context.CourseSubject.BuildParameter("CourseSubjectId", DbType.Int32, ParameterDirection.Output);
            
            context.CourseSubject.Execute("CourseSubject_Insert", context.CourseSubject.BuildParameter("CourseId", DbType.Int32, courseId),
                                            context.CourseSubject.BuildParameter("SubjectId", DbType.Int32, subjectId),
                                            context.CourseSubject.BuildParameter("GroupId", DbType.Int32, groupId),
                                            courseSubjectIdParam);

            return (int)courseSubjectIdParam.Value;
        }

        public ICollection<Subject> FindUnusedSubject(int courseId, string subjectCode,
                                                        string subjectName, int status,
                                                        int Offset, int MaxItem,
                                                        out int UnusedSubjectCount)
        {
            IDataParameter UnusedSubjectCountParam = context.CourseSubject.BuildParameter("UnusedSubjectCount", DbType.Int32, ParameterDirection.Output);
            

            var subjects = context.Subject.Init("CourseSubject_FindUnused",
                                            context.CourseSubject.BuildParameter("CourseId", DbType.Int32, courseId),
                                            context.CourseSubject.BuildParameter("SubjectCode", DbType.String, subjectCode),
                                            context.CourseSubject.BuildParameter("SubjectName", DbType.String, subjectName),
                                            context.CourseSubject.BuildParameter("Status", DbType.Int32, status),
                                            context.CourseSubject.BuildParameter("Offset", DbType.Int32, Offset),
                                            context.CourseSubject.BuildParameter("MaxItem", DbType.Int32, MaxItem),
                                            UnusedSubjectCountParam)
                                .MapCollection<Subject>()
                                .GetCollection();

            UnusedSubjectCount = (int)UnusedSubjectCountParam.Value;

            return subjects;
        }

        public bool IsCourseExist(int courseId)
        {
            var course = context.Course.Init("CourseSubject_IsCourseExist",
                                            context.CourseSubject.BuildParameter("CourseId", DbType.Int32, courseId))
                                .Map()
                                .Get();
            if (course == null)
                return false;

            return true;
        }

        public bool IsCourseDisabled(int courseId)
        {
            var course = context.Course.Init("CourseSubject_IsCourseExist",
                                            context.CourseSubject.BuildParameter("CourseId", DbType.Int32, courseId))
                                .Map()
                                .Get();
            return course.IsDisabled;
        }

        public bool IsSubjectDisabled(int subjectId)
        {
            var subject = context.Subject.Init("CourseSubject_IsSubjectDisabled",
                                            context.CourseSubject.BuildParameter("@SubjectId", DbType.Int32, subjectId))
                                .Map()
                                .Get();
            return subject.IsDisabled;
        }

        public bool IsDeletePossible(int courseId, int subjectId)
        {
            IDataParameter feedbackcount = context.CourseSubject.BuildParameter("count", DbType.Int32, ParameterDirection.Output);

            context.Course.Init("CourseSubject_IsDeletePossible",
                                            context.CourseSubject.BuildParameter("CourseId", DbType.Int32, courseId),
                                            context.CourseSubject.BuildParameter("SubjectId", DbType.Int32, subjectId),
                                            feedbackcount).Map().Get();

            if ((int)feedbackcount.Value == 0)
                return true;

            return false;
        }
    }
}
